//这个文件专门用来存放关于后台药品管理页面的全部接口
const Router = require("koa-router");
const home = new Router();
const { exec } = require("../util/db");
// const db = require('../util/db')
const bodyparser = require("koa-bodyparser");
home.use(bodyparser()); //调用这个中间件 就可以拿到前端post过来的数据

// 专供后台的get请求
// http://localhost:3000/drug/list
home.get("/list", async (ctx) => {
  let obj = ctx.query;
  console.log("======", obj);
  if (JSON.stringify(obj) == "{}") {
    let selectSql = `select * from drug_information`;
    var res = await exec(selectSql);
    // ctx.body = res;
  } else {
    let inquiry = "";
    for (var p in obj) {
      if (inquiry != "") [(inquiry += "and")];
      inquiry += ` ${p}='${obj[p]}'`;
    }
    let selectSql = `select * from drug_information where${inquiry}`;
    console.log("----", selectSql);
    var res = await exec(selectSql);
    // ctx.body = res;
  }
  for (var i = 0; i < res.length; i++) {
    res[i].drugImg = `http://localhost:8080/api${res[i].drugImg}`;
  }
  ctx.body = res;
});

// http://localhost:3000/drug/classifyList
home.get("/classifyList", async (ctx) => {
  let selectSql = `select drugClassification from drug_information`;
  let res = await exec(selectSql);
  for (var i = 0; i < res.length; i++) {
    res[i] = res[i].drugClassification;
  }
  res = Array.from(new Set(res));
  ctx.body = res;
});

// http://localhost:3000/drug/brandList
home.get("/brandList", async (ctx) => {
  let selectSql = `select drugBrand from drug_information`;
  let res = await exec(selectSql);
  for (var i = 0; i < res.length; i++) {
    res[i] = res[i].drugBrand;
  }
  res = Array.from(new Set(res));
  ctx.body = res;
});

function getRandom() {
  let code = "";
  for (var i = 0; i < 6; i++) {
    code += parseInt(Math.random() * 10);
  }
  return code;
}

// post请求
// http://localhost:3000/drug/add
home.post("/add", async (ctx) => {
  console.log("57---obj", ctx);
  let obj = ctx.request.body;
  let id = getRandom();
  let insertSql =
    `insert into drug_information(drugId,drugName,drugImg,drugClassification,
              drugBrand,producer,indication,usageMethod,pharmacologicalEffect,
              mattersNeedingAttention,composition) 
              values(` +
    `'${id}','${obj.drugName}','${obj.drugImg}','${obj.drugClassification}',
              '${obj.drugBrand}','${obj.producer}','${obj.indication}','${obj.usageMethod}',
              '${obj.pharmacologicalEffect}','${obj.mattersNeedingAttention}','${obj.composition}')`;
  console.log(insertSql);

  await exec(insertSql);
  ctx.body = "插入成功啦";
});

// http://localhost:3000/drug/edit
home.post("/edit", async (ctx) => {
  let obj = ctx.request.body;
  let updateSql = `update drug_information 
          set 
          drugName='${obj.drugName}',
          drugImg='${obj.drugImg}' ,
          drugClassification='${obj.drugClassification}' ,
          drugBrand='${obj.drugBrand}' ,
          producer='${obj.producer}' ,
          indication='${obj.indication}' ,
          usageMethod='${obj.usageMethod}' ,
          pharmacologicalEffect='${obj.pharmacologicalEffect}' ,
          mattersNeedingAttention='${obj.mattersNeedingAttention}' ,
          composition='${obj.composition}' 
          where 
          drugId='${obj.drugId}'`;
  await exec(updateSql);
  ctx.body = "更新成功啦";
});

// delete请求
// http://localhost:3000/drug/del
home.delete("/del", async (ctx) => {
  let obj = ctx.request.body;
  let deleteSql = `delete from drug_information where drugId='${obj.drugId}'`;
  await exec(deleteSql);
  ctx.body = "删除成功啦";
});

module.exports = home;
